<template>

	<view class="main">
		<van-toast id="van-toast" />
		<view class="hand1">
			<image :src="wechat_view.logo" @click="wxLogin"></image>
			<view class="htap">

				<view class="htapone" @click="wxLogin">
					<view class="honeleft">
						<image src="https://qiniu.jxyutuo.com/yanqin/tabBarimg/81.png"></image>
						账户充值
					</view>
					<image src="https://qiniu.jxyutuo.com/yanqin/tabBarimg/9.png"></image>
				</view>


				<view class="htapone" @click="wxLogin">
					<view class="honeleft">
						<image src="https://qiniu.jxyutuo.com/yanqin/tabBarimg/101.png"></image>
						电卡管理
					</view>
					<image src="https://qiniu.jxyutuo.com/yanqin/tabBarimg/11.png"></image>
				</view>


				<view class="htapone" @click="wxLogin">
					<view class="honeleft">
						<image src="https://qiniu.jxyutuo.com/yanqin/tabBarimg/121.png"></image>
						附近电站
					</view>
					<image src="https://qiniu.jxyutuo.com/yanqin/tabBarimg/13.png"></image>
				</view>

			</view>
		</view>
		<view class="kant">
			<view class="ddtap">
				<image src="https://qiniu.jxyutuo.com/yanqin/tabBarimg/ddc.png"></image>
			</view>

			<view class="saoma" @click="wxLogin" v-if="cetype == 1">
				<image src="https://qiniu.jxyutuo.com/yanqin/tabBarimg/login_icon.png"></image>
				<view class="smtxt">一键登录</view>
			</view>
			
			<view class="saoma" @click="localLogin" v-if="cetype == 2">
				<image src="https://qiniu.jxyutuo.com/yanqin/tabBarimg/login_icon.png"></image>
				<view class="smtxt">一键登录</view>
			</view>
			
		</view>

		<tabBar tab01="2"></tabBar>

	</view>

</template>

<script>
	import tabBar from '../../components/tabBar/tabBar.vue'
	import {
		Config
	} from '../../common/Config.js'
	export default {
		components: {
			tabBar
		},
		data() {
			return {
				wechat_view: [], //公众号信息
				code: '',
				deviceId: '', //设备号
				port: '', //端口号
				cetype: 1,//1线上版本，2本地版本
			}
		},
		onLoad(options) {
			if (options.deviceId) {
				this.deviceId = options.deviceId;
				uni.setStorageSync('scan_deviceId', options.deviceId);
			}
			if (options.port) {
				this.port = options.port
				uni.setStorageSync('scan_port', options.port);
			}
		},
		onShow() {
			let that = this;
			//uni.clearStorageSync();
			//that.get_wechat_view();
			that.wxLogin();
		},
		methods: {

			//获取会员所处公众号的信息
			get_wechat_view() {
				let that = this;
				let data = {
					appKey: Config.appkey
				}
				that.$base.request('wechat/view', 'GET', data)
					.then(res => {
						if (res.data.code == 200) {
							var view = res.data.data;
							if (view.logo) {
								view.logo = Config.imgapi + view.logo;
							}
							if (view.qrcode) {
								view.qrcode = Config.imgapi + view.qrcode;
							}
							that.wechat_view = view;
							uni.setStorageSync('wechat_view', view);
						}
					})
					.catch(err => {

					})
			},
			
			//登录
			wxLogin() {
				let that = this;
				uni.login({
					"provider": "weixin",
					"onlyAuthorize": true, // 微信登录仅请求授权认证
					success: function(event){
						console.log(event);
						const code = event.code;
						let data = {
							appKey: Config.appkey,
							code: code
						}
						if(that.deviceId){
							data.deviceId = that.deviceId;
						}
						that.$base.request('wxlogin/wxapp/getcode', 'POST', data)
							.then(res => {
								if (res.data.code == '200') {
									uni.setStorageSync('userToken', res.data.data.token);
									that.getUserInfo();
								}
								else if(res.data.code == '301'){
									var appid = res.data.data.appid;
									var wxapp_openid = res.data.data.wxapp_openid;
									if(res.data.data.project_id == null){
										var project_id = '';
									}
									else{
										var project_id = res.data.data.project_id;
									}
									uni.redirectTo({
										url: '/pages/offical_login/offical_login?appid='+appid+'&wxapp_openid='+wxapp_openid+'&project_id='
									})
								}
								else {
									uni.showModal({
									    title: '提示',
									    content: res.data.msg,
										showCancel: false,
									    success: function (cres) {
									        if (cres.confirm) {
									            uni.reLaunch({
									            	url: '/pages/login/login'
									            })
									        }
									    }
									});
								}
							})
							.catch(err => {})
					},
					fail: function (err) {
				        // 登录授权失败
				        // err.code是错误码
				    }
				})
			},
			
			//本地登录
			localLogin() {
				let that = this;
				var userId = 5;
				let data = {
					userId: userId
				}
				this.$base.request('test/login', 'POST', data)
					.then(res => {
						uni.setStorageSync('userToken', res.data.data.token);
						that.getUserInfo();
					})
					.catch(err => {
						this.$base.hideLoading()
						this.$base.prompt('温馨提示', '网络不给力,稍后请重新尝试', () => {}, () => {})
					})
				
			},
			
			getUserInfo() {
				let that = this;
				that.$base.request('user/userInfo', 'GET')
					.then(res => {
						if (res.data.code == 200) {
							that.userlist = res.data.data
							uni.setStorageSync('userId', that.userlist.id)
							that.userlist.id = that.pad(that.userlist.id, 8)
							uni.setStorageSync('user', that.userlist)
							uni.setStorageSync('phone', that.userlist.phone)
							uni.setStorageSync('Name', that.userlist.realName)
							if (res.data.data.subscribe == 0) {
								// uni.redirectTo({
								// 	url: '/pages/qr_code/qr_code'
								// })
								// return
							}
							if(uni.getStorageSync('scan_deviceId') != '' && uni.getStorageSync('scan_port') != ''){
								var scan_deviceId = uni.getStorageSync('scan_deviceId');
								var scan_port = uni.getStorageSync('scan_port');
								uni.setStorageSync('scan_deviceId', '');
								uni.setStorageSync('scan_port', '');
								uni.navigateTo({
									url: '/pages/poweron/poweron?device=' + scan_deviceId + '&port=' + scan_port +
										'&cmd=' + 0 + '&from=' + 1,
								});
							}
							else{
								uni.redirectTo({
									url: '/pages/smcharging/smcharging',
								});
							}
						}
					})
					.catch(err => {
				
					})
			},
			
			pad(num, n) {
				var len = num.toString().length;
				while (len < n) {
					num = "0" + num;
					len++;
				}
				return num;
			},
			
			onShareAppMessage(res) {
				return {
					title: '欢迎使用'+uni.getStorageSync('wechat_view').wechatName,
					path: 'pages/login/login',
					imageUrl: uni.getStorageSync('wechat_view').logo,
				}
			},
			
		},

	}
</script>
<style lang="scss" scoped>
	page {
		background-color: #2A3034
	}

	.main {
		width: 100%;
		overflow-x: hidden;
		background-color: #2A3034;
		height: 100vh;

		.foothand {
			position: absolute;
			z-index: 9;
			top: 0upx;
			left: 5%;
			width: 90%;
			background: linear-gradient(180deg, #8BC21C 0%, #A8D628 100%);
			border-radius: 24upx;
			padding: 10upx 24upx;
			box-sizing: border-box;
			margin-top: 30upx;
			color: #fff;
		}

		.gztan {
			width: 650upx;
			background-color: #FFFFFF;
			text-align: center;
			padding: 70upx 0;

			.gztanimg {
				width: 350upx;
				height: 350upx;
				margin: 0 auto;

				img {
					width: 350upx;
					height: 350upx;
				}
			}

			.gztantxt {
				margin-top: 30upx;
				font-size: 20upx;
			}
		}

		.hand {
			width: 100%;
			height: 250upx;
			text-size-adjust: 100% !important;
			-webkit-text-size-adjust: 100% !important;
			-moz-text-size-adjust: 100% !important;
			background: url('https://qiniu.jxyutuo.com/yanqin/tabBarimg/indexhand1.png') no-repeat;
			// background-color: blue;
			// border-radius: 0 0 5% 5%;
			// background-image: linear-gradient(to right, #5260Ef , #428DE9);
			background-size: cover;
			box-sizing: border-box;
			padding-top: 50upx;
			position: relative;
			margin-bottom: 650upx;

			image {
				width: 177upx;
				height: 80upx;
				// margin-top: 80upx;
				margin-left: 36upx;
			}

			.htap {
				width: 100%;
				position: absolute;
				height: 700upx;
				left: 5%;
				margin-top: 90upx;

				.htapone {
					padding: 0 36upx;
					box-sizing: border-box;
					width: 90%;
					height: 200upx;
					background: #3A4044;
					border-radius: 6upx;
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 32upx;

					image {
						width: 146upx;
						height: 146upx;
						margin: 0;
					}

					.honeleft {
						font-size: 34upx;
						font-family: PingFangSC-Medium, PingFang SC;
						font-weight: 500;
						color: #FFFFFF;
						display: flex;
						align-items: center;

						image {
							width: 160upx;
							height: 160upx;
							margin: 0 auto;
							margin-right: 24upx;
						}
					}
				}
			}
		}

		.hand1 {
			width: 100%;
			height: 250upx;
			text-size-adjust: 100% !important;
			-webkit-text-size-adjust: 100% !important;
			-moz-text-size-adjust: 100% !important;
			background: url('https://qiniu.jxyutuo.com/yanqin/tabBarimg/indexhand1.png') no-repeat;
			// background-color: blue;
			// border-radius: 0 0 5% 5%;
			// background-image: linear-gradient(to right, #5260Ef , #428DE9);
			background-size: cover;
			box-sizing: border-box;
			position: relative;
			margin-bottom: 650upx;

			image {
				width: 177upx;
				height: 177upx;
				// margin-top: 80upx;
				margin-left: 36upx;
			}

			.htap {
				width: 100%;
				position: absolute;
				height: 700upx;
				left: 5%;
				margin-top: 40upx;

				.htapone {
					padding: 0 36upx;
					box-sizing: border-box;
					width: 90%;
					height: 200upx;
					background: #3A4044;
					border-radius: 6upx;
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 32upx;

					image {
						width: 146upx;
						height: 146upx;
						margin: 0;
					}

					.honeleft {
						font-size: 34upx;
						font-family: PingFangSC-Medium, PingFang SC;
						font-weight: 500;
						color: #FFFFFF;
						display: flex;
						align-items: center;

						image {
							width: 160upx;
							height: 160upx;
							margin: 0 auto;
							margin-right: 24upx;
						}
					}
				}
			}
		}

		.kant {
			width: 100%;
			box-sizing: border-box;
			padding: 0 36upx;
			margin-bottom: 140upx;

			.ddtap {
				width: 100%;
				height: 251upx;
				margin: 0 auto;

				image {
					width: 100%;
					height: 251upx;
				}

				video {
					width: 100%;
					height: 251upx;
				}
			}

			.saoma {
				width: 678upx;
				display: flex;
				height: 110upx;
				background: linear-gradient(180deg, #8BC21C 0%, #A8D628 100%);
				box-shadow: 0px 5px 24px 0px rgba(139, 195, 28, 0.4);
				border-radius: 60upx;
				margin: 0 auto;
				align-items: center;
				justify-content: center;
				margin-top: 30upx;

				image {
					width: 63upx;
					height: 63upx;
				}

				.smtxt {
					font-size: 36upx;
					font-family: PingFangSC-Medium, PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
					margin-left: 24upx;
				}

			}

			.yctap {
				width: 100%;
				text-align: center;
				font-size: 26upx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #FFFFFF;
				margin-top: 25upx;
			}
		}

		.tan {
			width: 600upx;
			padding: 100upx 0;

			.xz {
				text-align: center;
				padding: 0 30upx;

				image {
					width: 300upx;
					height: 289upx;
				}

				.xzone {
					margin-top: 28upx;
					font-weight: bold;
					color: #000000;
					margin-bottom: 80upx;
					font-size: 38upx;

				}

				.xztwo {
					margin-top: 15upx;
					font-weight: 600;
					color: #90c038;
					font-size: 30upx;
				}

				.xzthree {
					margin-top: 30upx;
					font-weight: 600;
					color: #4e4e4e;
					font-size: 20upx;
					margin-bottom: 40upx;
				}
			}

			.anniu {
				display: flex;
				justify-content: space-between;
				padding: 0 90upx;
				font-weight: 600;
				font-size: 28upx;
				color: #333333;

				.anniu1 {
					width: 130upx;
					height: 55upx;
					line-height: 55upx;
					border-radius: 40upx;
					text-align: center;
					border: #333333 2upx solid;
				}
			}
		}

		.tishi {
			.tstou {
				color: #333333;
				font-size: 32upx;
				font-family: PingFang SC Bold, PingFang SC Bold-Bold;
				font-weight: 700;
				text-align: center;
			}

			.tsnei {
				font-size: 28upx;
				padding: 30upx;
				color: #555555;
			}

			.kbtn {
				width: 570upx;
				height: 75upx;
				background: #4b98ed;
				font-size: 30upx;
				border-radius: 16upx;
				font-family: PingFang SC Bold, PingFang SC Bold-Bold;
				font-weight: 700;
				text-align: center;
				line-height: 75upx;
				margin: 0 auto;
				margin-top: 40upx;
				color: #ffffff;
			}
		}
	}
</style>